﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameGo.Attribute;
using Model;
using BLL;
using PagedList;
using System.Text.RegularExpressions;

namespace GameGo.Controllers
{
    public class AdminController : Controller
    {
        goodsBLL goodContext = new goodsBLL();
        usersBLL userContext = new usersBLL();
        ordersBLL ordersContext = new ordersBLL();
        // GET: Admin
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string pwd)
        {
            if (pwd == "123456")
            {
                Session["AdminID"] = "123";
                return RedirectToAction("Index");
            }
            else
                return View();
        }
        [IsAdminLogined]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult logout()
        {
            Session["AdminID"] = null;
            return View("Login");
        }
        //用户权限
        public ActionResult UserPower(int? page)
        {
            var List = userContext.GetAllUser().OrderBy(o => o.ID);
            int pageNumber = page ?? 1;
            int pageSize = 10;
            return View(List.ToPagedList(pageNumber, pageSize));
        }
        //上架
        public ActionResult GoodUpload()
        {
            var list = goodContext.GetTypeList().ToList().Select(x => new SelectListItem { Text = x.typeName, Value = x.ID.ToString() }).ToList();
            SelectList sellist = new SelectList(list, "ID", "typeName");
            ViewData["typeid"] = list;
            return View();
        }
        public ActionResult UploadGood(goods good)
        {
            HttpPostedFileBase imageName = Request.Files["goodImage"];
            string file = null;
            try
            {
                file = imageName.FileName;
            }
            catch (Exception)
            {
                return Content("<script>alert('图片未能正常上传！');history.go(-1);</script>");
            }
            string fileFormat = file.Split('.')[file.Split('.').Length - 1]; // 以“.”截取，获取“.”后面的文件后缀
            Regex imageFormat = new Regex(@"^(bmp)|(png)|(gif)|(jpg)|(jpeg)"); // 验证文件后缀的正则表达式
            // 验证后缀不合格
            if (string.IsNullOrEmpty(file) || !imageFormat.IsMatch(fileFormat))
            {
                return Content("<script>alert('发布失败！');");
            }
            // 验证后缀合格
            else
            {
                string timeStamp = DateTime.Now.Ticks.ToString(); // 获取当前时间的string类型
                string firstFileName = timeStamp.Substring(0, timeStamp.Length - 4); // 通过截取获得文件名
                string imageStr = "Content/images/goods/"; // 获取保存图片的项目文件夹
                string uploadPath = Server.MapPath("~/" + imageStr); // 将项目路径与文件夹合并
                string pictureFormat = file.Split('.')[file.Split('.').Length - 1];// 设置文件格式
                string fileName = firstFileName + "." + fileFormat;// 设置完整（文件名+文件格式） 
                string saveFile = uploadPath + fileName;//文件路径
                imageName.SaveAs(saveFile);// 保存文件
                string imageUrl = "goods/" + fileName;// 设置数据库保存的路径
                good.goodImage = imageUrl;
                good.nowState = 1;
                try
                {
                    goodContext.Add(good);
                    return Content("<script>alert('添加成功！');history.go(-1);</script>");
                }
                catch (Exception)
                {
                    return Content("<script>alert('发布失败！');history.go(-1);</script>");
                }
            }
        }
        //发货
        public ActionResult ExpressManage(int? page)
        {
            var List = goodContext.GetSendOrder().OrderByDescending(o => o.ID);
            int pageNumber = page ?? 1;
            int pageSize = 10;
            return View(List.ToPagedList(pageNumber, pageSize));
        }
        //下架
        public ActionResult GoodsManange(int? page)
        {
            var List = goodContext.GetAllGoods().OrderBy(o => o.ID);
            int pageNumber = page ?? 1;
            int pageSize = 10;
            return View(List.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult underCarriage(int? id)
        {
            int cur = id ?? 1;
            goodContext.ModifyState(cur);
            return RedirectToAction("GoodsManange");
        }
        public ActionResult sendGoods(int id)
        {
            ordersContext.ModifyState(id, 2);
            return RedirectToAction("ExpressManage");
        }
        public ActionResult KillUser(int? id)
        {
            int cur = id ?? 1;
            userContext.ModifyState(cur);
            return RedirectToAction("UserPower");
        }
    }
}